package com.ms.game.lobby;

import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ms.game.gmtool.WhiteListInfo;
import com.ms.game.lobby.bulletin.BullentinAllListInfo;
import com.ms.game.lobby.bulletin.BulletinModelInfo;
import com.ms.game.lobby.message.CLobbyHallMachinedataRequest;
import com.ms.game.lobby.message.CPlayerModifyOfflineKeepDataRequest;
import com.ms.game.lobby.terminatedaccountlist.GetTerminatedAccountListAsync;
import com.ms.game.lobby.terminatedaccountlist.message.CGetTerminatedAccountListRequest;
import com.ms.game.lobby.terminatedaccountlist.message.SGetTerminatedAccountListResponse;
import com.ms.game.mgdh.message.SDoorManCancelOfflineKeepDataBackRequest;
import com.ms.game.slot.base.message.CSlotCommonDmSetRecvSeatUpdatePageRequest;
import com.ms.player.Player;
import com.ms.service.error.ErrorCode;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.annotations.MessageType;
import com.xpec.c4.service.annotations.Name;
import com.xpec.c4.service.common.BaseMessageAction;
import com.xpec.c4.service.contexts.Contexts;
import com.xpec.c4.service.contexts.ScopeType;
import com.xpec.c4.service.message.MessageAction;
import com.xpec.c4.service.module.BasicServiceModule;

@Name(scopeType = ScopeType.SESSION)
public class GameLobbyAction extends BaseMessageAction {
	@SuppressWarnings("unused")
	private static final Log log = LogFactory.getLog(GameLobbyAction.class);

	public GameLobbyProcesser getProcessor(){
		BasicServiceModule module = getModule();
		if(module instanceof GameLobbyModule){
			GameLobbyModule gameLobbyModule = (GameLobbyModule) module;
			return gameLobbyModule.getGameLobby();
		}
		
		return null;
	}
	
	/**
	 * 請求大廳遊戲列表
	 */
	@MessageType(typeCode = MessageTypes.C_LOBBY_HALLTYPELIST_REQUEST)
	public void recvGameTypeListRequest(Object obj) {
		Player player = getPlayer();
		getProcessor().onGetHallTypeList(player ,getSessionID());
	}
	
	
	/**
	 * 確認玩家離線保留資料(2016-03-17 修改成抓取所有保留資料)
	 */
	@MessageType(typeCode = MessageTypes.C_PLAYER_CHECK_OFFLINEKEEPDATA_REQUEST)
	public void revPlayCheckOfflineKeepData(Object obj) {
		Player player = this.getPlayer();
		GUID session = this.getSessionID();
		
		GameLobbyProcesser.onCheckPlayerOfflineKeepData(this);
	}
	
	/**
	 *更新玩家離線保留資料
	 */
	@MessageType(typeCode = MessageTypes.C_PLAYER_MODIFY_OFFLINEKEEPDATA_REQUEST)
	public void revPlayerModifyOfflineKeepData(CPlayerModifyOfflineKeepDataRequest request) {
		GameLobbyProcesser.onModifyPlayerOfflineKeepData(request , this);
	}
	
	
	/**
	 *doorman玩家離線保留資料back
	 */
	@MessageType(typeCode = MessageTypes.S_DOORMAN_MODIFY_OFFLINEKEEPDATA_BACK)
	public void revGameDoorManModifyOfflineKeepDataBack(SDoorManCancelOfflineKeepDataBackRequest request) {
		GameLobbyProcesser.onModifyPlayerOfflineKeepDataBack(request , this);
	}

	/**
	 * 收到 client 要求領取自動回復幸運金
	 */
	@MessageType(typeCode = MessageTypes.C_LOBBY_GET_AUTO_RECOVER_LUCKY_COIN_REQUEST)
	public void recvGetAutoRecoverLuckyCoinRequest(Object object) {
		Player player = getPlayer();
		GameLobbyProcesser.handleGetAutoRecoverLuckyCoin(player);
	}
	
	/**
	 * GMTool 取得即時限上會員 
	 */
	@MessageType(typeCode = MessageTypes.S_GMTOOL_ONLINE_NUMBER_REQUEST)
	public ArrayList<GUID> getHallUserNumber(Object request){
		return GameLobbyProcesser.getHallUserNumber();
	}
	/**
	 * client 要求遊戲館的座位列表
	 */
	@MessageType(typeCode = MessageTypes.C_LOBBY_HALL_MACHINEDATA_REQUEST)
	public void recvLobbyHallMachinedataRequest(CLobbyHallMachinedataRequest request) {
		Player player = getPlayer();
		GameLobbyProcesser.handleHallMachinedataRequest(player, request);
	}
	/**
	 * GMTool修改白名單資料 
	 */
	@MessageType(typeCode = MessageTypes.S_GMTOOL_EDIT_WHITE_LIST)
	public void editGameWhiteList(WhiteListInfo info){
		GameLobbyProcesser.editGameWhiteList(info);
	}
	
	/**
	 * client 取得公告列表 
	 */
	@MessageType(typeCode = MessageTypes.C_BULLETIN_LIST)
	public void getBulletinList(long lastModifyTime){
		GUID session = this.getSessionID();
		GameLobbyProcesser.getBulletinTitleList(lastModifyTime,session);
	}
	
	/**
	 * Client登入時取得新的 系統公告 跟 活動公告 數量
	 * @param lastModifyTime
	 */
	@MessageType(typeCode = MessageTypes.C_BULLETIN_GET_NEW_COUNT_REQUEST)
	public void getBulletinListCount(long lastModifyTime){
		GUID session = this.getSessionID();
		GameLobbyProcesser.getBulletinTitleCount(lastModifyTime,session);
	}
	/**
	 * client 取得公告內容 
	 */
	@MessageType(typeCode = MessageTypes.C_BULLETIN_CONTENT)
	public void getBulletinContent(int bulletinNumber){
		GUID session = this.getSessionID();
		GameLobbyProcesser.getBulletinContent(bulletinNumber,session);
	}
	
	/**
	 * GMTool 修改server公告暫存 
	 */
	@MessageType(typeCode = MessageTypes.S_GMTOOL_UPDATE_BULLETIN)
	public void updateBulletin(BulletinModelInfo info){
		GameLobbyProcesser.updateBulletin(info);
	}
	
	/**
	 * GMTool 通知server重新載入DB公告資料到暫存 (萬ㄧ新增發生錯誤時可以不用重開就可以改暫存資料) 
	 */
	@MessageType(typeCode = MessageTypes.S_GMTOOL_UPDATE_SERVER_BULLETIN)
	public void updateBulletinList(BullentinAllListInfo info){
		GameLobbyModule module = (GameLobbyModule)Contexts.getModule();
		GameLobbyProcesser.updateAllBulletinListByGMTool(module,info);
	}
	
	/**
	 * 登入時取得跑馬燈公告資訊
	 */
	@MessageType(typeCode = MessageTypes.C_MARQUEE_LIST)
	public void getMarqueeList(long lastModifyTime){
		GUID sessionId = sessionID;
		GameLobbyProcesser.getGMToolMarqueeList(sessionId , lastModifyTime);
	}
	
	/**
	 * 登入時取得強制公告資訊
	 */
	@MessageType(typeCode = MessageTypes.C_FORCIBLY_LIST)
	public void getForciblyList(Object object){
		GUID sessionId = sessionID;
		GameLobbyProcesser.getGMToolForciblyList(sessionId);
	}
	
	/**
	 * 更新遊戲列表
	 */
	@MessageType(typeCode = MessageTypes.S_LOBBY_UPDATE_HALLTYPELIST)
	public void updateGameHallList(String resultKey){
		getProcessor().onCacheGameTypeData(resultKey);
	}
	
	
	/**
	 * client 指定要收哪一頁的座位更新資訊
	 * 
	 * @param request
	 */
	@MessageType(typeCode = MessageTypes.C_SLOT_COMMON_DM_SET_RECV_SEAT_UPDATE_PAGE_REQUEST)
	public void revSetRecvSeatUpdatePageRequest(CSlotCommonDmSetRecvSeatUpdatePageRequest request) {
		Player player = getPlayer();
		GameLobbyProcesser.handleSetRecvSeatUpdatePageRequest(player, request);
	}
	
	@MessageType(typeCode = MessageTypes.C_LOBBY_GET_TERMINATED_ACCOUNT_LIST_REQUEST)
	public void revGetPageRequest(CGetTerminatedAccountListRequest request) {
		Player player = getPlayer();
		if(player == null){
			if(log.isErrorEnabled()){
				log.error("player is not found");
			}
			return;
		}
		
		GUID playerId = player.getGuid();
		GUID sessionId = getSessionID();
		SGetTerminatedAccountListResponse response = new SGetTerminatedAccountListResponse();
//		if(!ActionBlocker.checkAndBlock(ActionBlockType.GET_TERMINATED_ACCOUNT_LIST, playerId.toString())){
//			response.setErrorCode(ErrorCode.INVALID_PARAM);
//			MessageAction.sendMessage(getSessionID(), MessageTypes.S_LOBBY_GET_TERMINATED_ACCOUNT_LIST_RESPONSE, response);
//			return;
//		}
		
		
		int numOfPage = request.getNumOfPage();
		if(request.getNumOfPage() <= 0){
			if(log.isErrorEnabled()){
				log.error("numOfPage error : " + numOfPage);
			}
			response.setErrorCode(ErrorCode.INVALID_PARAM);
			MessageAction.sendMessage(getSessionID(), MessageTypes.S_LOBBY_GET_TERMINATED_ACCOUNT_LIST_RESPONSE, response);
			return;
		}
		
		int page = request.getPage();
		if(page < 0){
			if(log.isErrorEnabled()){
				log.error("page error : " + page);
			}
			response.setErrorCode(ErrorCode.INVALID_PARAM);
			MessageAction.sendMessage(getSessionID(), MessageTypes.S_LOBBY_GET_TERMINATED_ACCOUNT_LIST_RESPONSE, response);
			return;
		}
		
		BasicServiceModule module = getModule();
		if (module instanceof GameLobbyModule) {
			GetTerminatedAccountListAsync async = new GetTerminatedAccountListAsync();
			async.setModuleServer(module);
			async.setPlayerId(playerId);
			async.setSessionId(sessionId);
			async.setRequest(request);
			async.key = playerId;
			module.addAsyncCommand(async);
		}
	}
	
}